<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { httpRequest } from "../../utils/http";
import {ref} from 'vue'
const item = ref('')
onLoad((e)=>{
    item.value = JSON.parse(e.item)
    console.log(item.value);
})

    //根据视频ID删除视频
    const delVideo = (e) => {
      uni.showModal({
        title: '提示',
        content: '确定要删除该视频吗?',
        showCancel: true,
        success: async({ confirm, cancel }) => {
          if(confirm){
            uni.showLoading({
              title: '正在删除',
              mask: true
            })
            await httpRequest("/api/delShopVideo", "POST", {
              videoId: e.videoId,
            });
            uni.hideLoading()
            uni.showToast({ title: "删除成功", icon: "none" });
            uni.navigateBack({ delta: 1 })
          }
        }
      })
      
    }
    //保存视频
    const saveVideo = (tempFilePath) => {
      uni.showLoading({
        title: "正在保存",
        mask: false,
      });
      //下载视频到本地
      uni.downloadFile({
        url: tempFilePath,
        success: ({ tempFilePath, statusCode }) => {
          //保存视频到相册
          uni.saveVideoToPhotosAlbum({
            filePath: tempFilePath,
            success: (res) => {
              console.log(res);
              uni.hideLoading();
              uni.showToast({
                title: "保存成功",
                icon: "success",
              });
            },
            fail: (error) => {
              console.log(error);
              uni.hideLoading();
              uni.showToast({
                title: "请先授权权限",
                icon: "error",
              });
              uni.authorize({
                scope: 'scope.writePhotosAlbum',
                success: (result) => {
                },
                fail: (error) => {
                  uni.showToast({
                title: "授权失败",
                icon: "error",
              });
                }
              })
            },
          });
        },
        fail: (error) => {
          console.log(error);
          uni.hideLoading();
          uni.showToast({
            title: "保存失败",
            icon: "error",
          });
        },
      });
    }

    const publish = () => {
        uni.navigateTo({ url: `/pageA/index/publish?item=${JSON.stringify(item.value)}` })
    }
</script>

<template>
  <view>
    <video style="width: 100%;height: 86%;position: fixed;" :src="item.videoUrl"></video>
    <view class="twobtn">
        <text class="advanced" @click="delVideo(item)">删除</text>
        <button class="quxiao1" @click="saveVideo(item.videoUrl)">保存视频</button>
        <button class="queren" @click="publish(item.videoId)">发布视频</button>
      </view>
  </view>
</template>

<style lang="scss">
    .twobtn {
    position: fixed;
    bottom: 0;
    background-color: #fff;
    width: 100%;
    height: 166rpx;
    display: flex;
    padding: 0 30rpx;
    padding-top: 10rpx;
    box-sizing: border-box;
    .advanced {
      font-size: 28rpx;
      color: rgb(144, 147, 153);
      margin-right: 34rpx;
      height: 88rpx;
      line-height: 88rpx;
    }
    
    .quxiao1 {
      width: 288rpx;
      height: 88rpx;
      text-align: center;
      line-height: 88rpx;
      color: rgba(142, 84, 244, 1);
      border: 1px solid rgba(142, 84, 244, 1);
      margin-right: 24rpx;
      background-color: #fff;
    }
    .queren {
      width: 288rpx;
      height: 88rpx;
      text-align: center;
      line-height: 88rpx;
      color: #fff;
      background-color: rgba(142, 84, 244, 1);
    }
  }
</style>

